package com.gljx.web.service.impl;

import com.gljx.constant.ActivitiKey;
import com.gljx.util.ActitiviUtil;
import com.gljx.util.GetFid;
import com.gljx.web.Entity.SubjectTwoReg;
import com.gljx.web.Entity.SubjectTwoRegentry;
import com.gljx.web.Entity.User;
import com.gljx.web.dao.CommonDao;
import com.gljx.web.dao.SubjectTwoRegMapper;
import com.gljx.web.dao.SubjectTwoRegentryMapper;
import com.gljx.web.service.CustomLogService;
import com.gljx.web.service.SubjectTwoRegService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
@Transactional
public class SubjectTwoRegServiceImpl extends CommonDao implements SubjectTwoRegService {
	
	@Autowired
	private SubjectTwoRegMapper subjectTwoRegMapper;
	
	@Autowired
	private SubjectTwoRegentryMapper subjectTwoRegentryMapper;

	@Autowired
	private CustomLogService customLogService;

	//导入activiti工具
	ActitiviUtil activitiUtil = new ActitiviUtil();
	
	@Override
	public Map<String, Object> selectPageCount(String psql) {
		// TODO Auto-generated method stub
		Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("Psql", psql);
        paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
        this.getSqlSession().select("com.gljx.web.dao.SubjectTwoRegMapper.pagecount", paramMap, null);
        return paramMap;
	}

	@Override
	public Map<String, Object> selectPage(int pindex, String psql, int psize) {
		// TODO Auto-generated method stub
		Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("Pindex", pindex);
        paramMap.put("Psql", psql);
        paramMap.put("Psize", psize);
        paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
        paramMap.put("v_cur", oracle.jdbc.OracleTypes.CURSOR);
        this.getSqlSession().select("com.gljx.web.dao.SubjectTwoRegMapper.pagemap", paramMap, null);
        return paramMap;
	}

	@Override
	public String selectFnumber(String companyId) {
		// TODO Auto-generated method stub
		return subjectTwoRegMapper.selectFnumber(companyId);
	}

	@Override
	public int addSubjectTwoReg(SubjectTwoReg subjectTwoReg) {
		// TODO Auto-generated method stub
		return subjectTwoRegMapper.insertSelective(subjectTwoReg);
	}

	@Override
	public int deleteByPrimaryKey(String fid) {
		// TODO Auto-generated method stub
		return subjectTwoRegMapper.deleteByPrimaryKey(fid);
	}

	@Override
	public SubjectTwoReg selSubjectTwoRegByFid(String fid) {
		// TODO Auto-generated method stub
		return subjectTwoRegMapper.selectByPrimaryKey(fid);
	}

	@Override
	public int updateSubjectTwoReg(User user,SubjectTwoReg subjectTwoReg,String [] subfid,String [] subfseq,String [] subcfzkzmbh,String [] subfnameL2,String [] substufnumber,String [] subfnumber,
			String [] subcfidcardno,String [] subcfsex,String [] subcfsjhm,String [] subcflxdh,String [] subcftjrName,String [] cfsqzjcxdh,String [] subcfjiaolianid,String [] subjlname,String [] subcfchexing
			,String [] subcflocal,String [] subcfkscs,String [] subcfckbk,String [] subcfbxwx,String [] subcfkmyjgsj,String [] subcfbjgsj,String [] subcfscts,String [] subcfclazz,String [] subcfysje,
			String [] subcfyhje,String [] subcfyisje,String [] subcfqfje,String [] subcfremark) {
		// TODO Auto-generated method stub
		int count=0;
		if(subjectTwoReg.getFid()!=null && !subjectTwoReg.getFid().equals("")){
			//修改父类	  	      
			count+=subjectTwoRegMapper.updateByPrimaryKeySelective(subjectTwoReg);			
			//删除父类对应的子类
			count+=subjectTwoRegentryMapper.deleteByParentId(subjectTwoReg.getFid());
			//增加子类
			for (int i = 0; i < subfid.length; i++) {
	        	SubjectTwoRegentry subjectTwoRegentry=new SubjectTwoRegentry();
	        	subjectTwoRegentry.setFid(GetFid.UUIDIntoFid());
	        	subjectTwoRegentry.setFparentid(subjectTwoReg.getFid());
	        	subjectTwoRegentry.setCfstudentid(subfid[i]);
	        	subjectTwoRegentry.setCfzkzmbh(subcfzkzmbh[i]);
	        	subjectTwoRegentry.setCfstudentno(substufnumber[i]);
	        	subjectTwoRegentry.setCfidcardno(subcfidcardno[i]);
	        	subjectTwoRegentry.setCfsex(Long.valueOf(subcfsex[i]));
	        	subjectTwoRegentry.setCfmobile(subcfsjhm[i]);
	        	subjectTwoRegentry.setCfphone(subcflxdh[i]);
	        	subjectTwoRegentry.setCfjsr(subcftjrName[i]);
	        	subjectTwoRegentry.setCfsqzjcxdh(cfsqzjcxdh[i]);
	        	subjectTwoRegentry.setCfjiaolianid(subcfjiaolianid[i]);
	        	subjectTwoRegentry.setJlzname(subjlname[i]);
	        	subjectTwoRegentry.setCfchexing(subcfchexing[i]);
	        	subjectTwoRegentry.setCfbdyd(Long.valueOf(subcflocal[i]));
	        	subjectTwoRegentry.setCfkscs(Long.valueOf(subcfkscs[i]));
	        	subjectTwoRegentry.setCfckbk(subcfckbk[i]);
	        	subjectTwoRegentry.setCfbxwx(subcfbxwx[i]);
	        	subjectTwoRegentry.setCfkmyjgsj(subcfkmyjgsj[i]);
	        	subjectTwoRegentry.setCfbjgsj(subcfbjgsj[i]);
	        	subjectTwoRegentry.setCfscts(Long.valueOf(subcfscts[i]));
	        	subjectTwoRegentry.setCfclazz(subcfclazz[i]);
	        	subjectTwoRegentry.setCfysje(new BigDecimal(subcfysje[i]));
	        	subjectTwoRegentry.setCfyhje(new BigDecimal(subcfyhje[i]));
	        	subjectTwoRegentry.setCfyisje(new BigDecimal(subcfyisje[i]));
	        	subjectTwoRegentry.setCfqfje(new BigDecimal(subcfqfje[i]));
	        	subjectTwoRegentry.setCfremark(subcfremark[i]);
	        	count+=subjectTwoRegentryMapper.insertSelective(subjectTwoRegentry);
	        	//添加服务日志
				customLogService.addLog("考试管理", "修改了学员:"+subjectTwoRegentry.getCfstudentno()+subjectTwoRegentry.getStudentName()+
						",科目二考试登记!车型:"+subjectTwoRegentry.getCfsqzjcxdh()+"教练:"+subjectTwoRegentry.getJlzname()+",考试日期:"+subjectTwoReg.getFbizdate()+",单据编号:"+subjectTwoReg.getFnumber(), subjectTwoRegentry.getCfstudentid(), user.getFid());
			}
			
		}		
		return count;
	}
	
	//审核
	@Override
	public int updateSubjectTwoReg(SubjectTwoReg subjectTwoReg) {
		// TODO Auto-generated method stub
		return subjectTwoRegMapper.updateByPrimaryKeySelective(subjectTwoReg);
	}

	@Override
	public List<SubjectTwoReg> selSubjectTwoRegByfbizdate(String companyid,String fbizdate, String cfcartype) {
		// TODO Auto-generated method stub
		return subjectTwoRegMapper.selSubjectTwoRegByfbizdate(companyid,fbizdate, cfcartype);
	}

	@Override
	public List<SubjectTwoReg> selSubjectTwoSumEnterPersons(String companyId, String beginTime, String endTime) {
		// TODO Auto-generated method stub
		return subjectTwoRegMapper.selSubjectTwoSumEnterPersons(companyId, beginTime, endTime);
	}

	@Override
	public List<SubjectTwoReg> selSubjectTwoResultPassExamRate(String companyId, String beginTime, String endTime) {
		// TODO Auto-generated method stub
		return subjectTwoRegMapper.selSubjectTwoResultPassExamRate(companyId, beginTime, endTime);
	}

	@Override
	public List<SubjectTwoReg> selSubjectTwoSumEnterPersonsByMonth(String companyId, String beginTime, String endTime) {
		// TODO Auto-generated method stub
		return subjectTwoRegMapper.selSubjectTwoSumEnterPersonsByMonth(companyId, beginTime, endTime);
	}

	@Override
	public List<SubjectTwoReg> selSubjectTwoResultPassExamRateByMonth(String companyId, String beginTime,
			String endTime) {
		// TODO Auto-generated method stub
		return subjectTwoRegMapper.selSubjectTwoResultPassExamRateByMonth(companyId, beginTime, endTime);
	}

	@Override
	public String selectAuditPerson(User user,Object fnumber) {
		//2.查询
		String result = activitiUtil.getTaskStateByUser(
				ActivitiKey.Driving_GL_ID.equals(user.getFcompanyid()) ? ActivitiKey.SUBJECTTWOREG_KEY_GL : ActivitiKey.SUBJECTTWOREG_KEY_NC,
				fnumber                  //单据
		);
		if (activitiUtil.notTask.equals(result)) {
			System.out.println("该单据没有对应流程");
			return "noTask";
		} else if (activitiUtil.FINISH.equals(result)) {
			System.out.println("流程已结束");
			return "finish";
		}//剩下两种是该任务进行中
		else if (activitiUtil.notAssign.equals(result)) {
			System.out.println("经办人不匹配");
			return "noAssign";
		} else if (activitiUtil.ISPOWER.equals(result)) {
			System.out.println("当前用户可以审核该单据");
			return "power";
		} else {
			return "failed";
		}
	}

	@Override
	public List<SubjectTwoReg> selectSubjectTwoReg(String companyId, String fnumber, String fdescription, String beginTime, String endTime) {
		Map<String,String> map =new HashMap<String,String>();
		map.put("companyId",companyId);
		map.put("fnumber",fnumber);
		map.put("fdescription",fdescription);
		map.put("beginTime",beginTime);
		map.put("endTime",endTime);
		return subjectTwoRegMapper.selectSubjectTwoReg(map);
	}
}
